package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.MiaoSha;
import com.team.six.bluecatshop.domain.Product;
import com.team.six.bluecatshop.service.MiaoShaService;
import com.team.six.bluecatshop.service.ProductService;
import com.team.six.bluecatshop.service.SkuService;
import com.team.six.bluecatshop.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/miaosha")
public class MiaoShaController {
    @Autowired
    ProductService productService;
    @Autowired
    MiaoShaService miaoShaService;
    @Autowired
    SkuService skuService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 秒杀商品细节
     * */
    @GetMapping("/miaosha-details")
    public String miaoShaDetails(String miaoshaId, String productId, Model model){

        MiaoSha miaoSha = miaoShaService.findById(miaoshaId);
        System.out.println(miaoshaId+ "," + productId);
//        redisUtil.dset("miaosha"+miaoshaId,miaoSha.getMiaoshaCount());
        redisUtil.set("miaosha"+miaoshaId,miaoSha.getMiaoshaCount());
        Product product=productService.findById(productId);//根据传来的id找到对应商品

        product=productService.setSkuToProduct(product);
        miaoSha.setProduct(product);
        //System.out.println(redisUtil.get("miaosha"+miaoshaId));
        Date timeEnd = miaoSha.getTimeEnd();
        Date now = new Date();
        if (now.after(timeEnd)){
            model.addAttribute("msg","late");
        }
        model.addAttribute("miaoSha",miaoSha);

        return "seckillDetails";
    }

    /**
     * 跳转秒杀列表
     * */
    @RequestMapping("/miaosha-list")
    public String miaoShaList(Model model){
        List<MiaoSha> miaoShas  = miaoShaService.findAll();
        for (MiaoSha a:miaoShas){
            Product product = productService.findById(a.getProductId());
            product=productService.setImgToProduct(product);
            a.setProduct(product);
        }
        model.addAttribute("miaoShas",miaoShas);
        return "seckillList";
    }

    /**
     * 秒杀验证库存，用于Ajax请求，在页面加载时判断是否还有库存
     * */
    @RequestMapping("/miaosha-verify")
    public String miaoshaVerify(String miaoshaId){
        if (miaoShaService.verifyCount(miaoshaId)){
            return "success";
        }else return "error";
    }

    /**
     * 处理numAdd请求
     */
    @RequestMapping("/numAdd")
    @ResponseBody
    public String numAdd(String num,String miaoshaId){
        //System.out.println(num);
        //System.out.println(miaoshaId);
        MiaoSha miaoSha=miaoShaService.findById(miaoshaId);
        if(Integer.parseInt(num) >= miaoSha.getMiaoshaCount()){
            return num;
        }else{
            return "";
        }
    }

    /**
     * 处理numReduce请求
     */
    @RequestMapping("/numReduce")
    @ResponseBody
    public String numReduce(String num){
        //System.out.println(num);
        if(Integer.parseInt(num) <= 1){
            return num;
        }else{
            return "";
        }
    }
    /**
     * 删除秒杀活动
     * */
    @RequestMapping("/miaoshaDelete")
    @ResponseBody
    public String miaoshaDelete(String miaoshaId){

        MiaoSha miaoSha = miaoShaService.findById(miaoshaId);
        Date timeEnd = miaoSha.getTimeEnd();
        Date nowTime = new Date();
        if(nowTime.after(timeEnd)){
            miaoShaService.remove(miaoshaId);
        }

        return "success";
    }

}
